#include <iostream>
#include <math.h>

#include "Array_t.h"
#include "Dlist_t.h"

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

using namespace std;

void testArray() {
	
	cout << "Tests on Array_t (T=int):" << endl;
	
	Array_t<int> arr1;
	Array_t<int> arr2; 

	cout << "count: " << arr1.count() << endl;  //should be: 0

	int* a = 0;
	int* b = 0;

	for(int i=1; i<=5; i++) {

		a = new int(i);
		b = new int(10+i);

		arr1.insert(a);
		arr2.insert(b);
	}

	a = new int(50);
	arr1.append(a,0);

	a = new int(100);
	arr1.prepend(a,arr1.count()-1);

	//should print: 1 50 2 3 4 100 5 
	for(int i=0; i<arr1.count(); i++) {
		cout << *arr1[i] << " ";  
	}

	cout << endl;  

	cout << "count: " << arr1.count() << endl;  //should be: 7

	int* f = arr1.find(50);
	cout << "found 50: " << *f << endl;  

	f = arr1.find(200);
	cout << "not found 200: " << f << endl;  //f is null ptr.

	arr1.remove(47);  //does nothing.

	arr1.remove(50);  
	cout << "count: " << arr1.count() << endl;  //should be: 6

	arr1.removeAndDeleteAll();
	arr2.removeAndDeleteAll();

	cout << endl;

}

void testDlist() {
	
	cout << "Tests on Dlist_t (T=int):" << endl;
	
	Dlist_t<int> lst1;
	Dlist_t<int> lst2; 

	cout << "count: " << lst1.count() << endl;  //should be: 0

	int* a = 0;
	int* b = 0;

	for(int i=1; i<=5; i++) {

		a = new int(i);
		b = new int(10+i);

		lst1.insert(a);
		lst2.insert(b);
	}

	a = new int(50);
	lst1.append(a,0);

	a = new int(100);
	lst1.prepend(a,lst1.count()-1);

	//should print: 1 50 2 3 4 100 5 
	int* c = lst1.Reset();
	while (c != 0) {
		cout << *c << " ";  
		c = lst1.Next();
	}

	cout << endl;  

	cout << "count: " << lst1.count() << endl;  //should be: 7

	int* f = lst1.find(50);
	cout << "found 50: " << *f << endl;  

	f = lst1.find(200);
	cout << "not found 200: " << f << endl;  //f is null ptr.

	lst1.remove(47);  //does nothing.

	lst1.remove(50);  
	cout << "count: " << lst1.count() << endl;  //should be: 6

	lst1.removeAndDeleteAll();
	//lst2.removeAndDeleteAll();

	cout << endl;
}

int main() {  
	
	testArray();
	
	cout << endl;

	testDlist();

	cout << _CrtDumpMemoryLeaks();

	getchar();

	return 0;

}

